!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

!==================================================================================================================================|
subroutine data_merge(var, x_base, y_base, z_base,                                                                                 &
                  ro, gp, mp, te, beta, vx, vy, vz, bx, by, bz, jx, jy, jz, fgx, fgy, fgz,                                         &
                  fggx, fggy, fggz, fmgx, fmgy, fmgz, fmtx, fmty, fmtz, x, y, z,                                                   &
                  var_flag, p_pos, n_var, nx_base, ny_base, nz_base, nx_block, ny_block, nz_block,                                 &
                  nx_no_bnd, ny_no_bnd, nz_no_bnd, ng, n_var_total, n_dims)
!==================================================================================================================================|

   implicit none

   include 'data_manage_module.include'

   integer(4), intent(in) :: n_dims, n_var, nx_base, ny_base, nz_base, nx_block, ny_block, nz_block,                               &
      nx_no_bnd, ny_no_bnd, nz_no_bnd, ng, n_var_total
   integer(4), intent(in) :: p_pos(n_dims)
   logical(4), intent(in) :: var_flag(n_var_total)

   real(8), intent(in) :: x(nx_block), y(ny_block), z(nz_block)
   real(output_real8_flag), dimension(nx_block, ny_block, nz_block), intent(in) :: ro, gp, mp, te, beta, vx, vy, vz, bx, by, bz,   &
      jx, jy, jz, fgx, fgy, fgz, fggx, fggy, fggz, fmgx, fmgy, fmgz, fmtx, fmty, fmtz
   real(8), intent(inout) :: x_base(nx_base), y_base(ny_base), z_base(nz_base)
   real(output_real8_flag), intent(inout) :: var(n_var, nx_base, ny_base, nz_base)

   integer(4) :: nx_tmp, ny_tmp, nz_tmp, i, j, k, i_c, j_c, k_c, i_p, j_p, k_p, i_var, j_var

!----------------------------------------------------------------------------------------------------------------------------------|
!  ro, gp, mp, te, beta, vx, vy, vz, vv, bx, by, bz, bv, jx, jy, jz, jv, fgx, fgy, fgz, fgv, fggx, fggy, fggz, fggv, 
!  fmgx, fmgy, fmgz, fmgv, fmtx, fmty, fmtz, fmtv
!----------------------------------------------------------------------------------------------------------------------------------|
   nx_tmp = (p_pos(1) - 1) * nx_no_bnd + ng
   ny_tmp = (p_pos(2) - 1) * ny_no_bnd + ng
   nz_tmp = (p_pos(3) - 1) * nz_no_bnd + ng

   do i = 0, nx_no_bnd
      i_c = i + ng
      i_p = i + nx_tmp
      x_base(i_p) = x(i_c)
   enddo

   do j = 0, ny_no_bnd
      j_c = j + ng
      j_p = j + ny_tmp
      y_base(j_p) = y(j_c)
   enddo

   do k = 0, nz_no_bnd
      k_c = k + ng
      k_p = k + nz_tmp
      z_base(k_p) = z(k_c)
   enddo

   do k = 0, nz_no_bnd
      k_c = k + ng
      k_p = k + nz_tmp
      do j = 0, ny_no_bnd
         j_c = j + ng
         j_p = j + ny_tmp
         do i = 0, nx_no_bnd
            i_c = i + ng
            i_p = i + nx_tmp
            i_var = 1
            j_var = 1

            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = ro(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = gp(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = mp(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = te(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = beta(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = vx(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = vy(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = vz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = vx(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = vy(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = vz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = bx(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = by(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = bz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = bx(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = by(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = bz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = jx(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = jy(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = jz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = jx(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = jy(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = jz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fgx(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fgy(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fgz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fgx(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = fgy(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = fgz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fggx(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fggy(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fggz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fggx(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = fggy(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = fggz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fmgx(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fmgy(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fmgz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fmgx(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = fmgy(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = fmgz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fmgx(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fmgy(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fmgz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
            j_var = j_var + 1
            if (var_flag(j_var)) then
               var(i_var, i_p, j_p, k_p) = fmtx(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = fmty(i_c, j_c, k_c)
               i_var = i_var + 1
               var(i_var, i_p, j_p, k_p) = fmtz(i_c, j_c, k_c)
               i_var = i_var + 1
            endif
         enddo
      enddo
   enddo

!----------------------------------------------------------------------------------------------------------------------------------|
   return
end subroutine data_merge
